package com.applink.sell_proof;

import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.api.Methods;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.constant.IntentKey;
import com.applink.sell_proof.model.Quiz;
import com.applink.sell_proof.model.QuizResultContent;

public class QuizResult extends BaseActionBarActivity implements Observer,OnClickListener{
	private Quiz mQuiz;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.quiz_result);
		mQuiz = (Quiz) getIntent().getSerializableExtra(Constant.KEY_QUIZ);
		setTitle("Quiz Winner");
		((TextView)findViewById(R.id.quiz_name)).setText(mQuiz.getName()); 
		((TextView)findViewById(R.id.quiz_description)).setText(mQuiz.getDescription()); 
		DataHelper.getInstance().addObserver(this);
		DataHelper.getInstance().getQuizResult(mQuiz.getId());
		DataHelper.getInstance().getPresetAnswerQuiz(mQuiz.getId());
		showProgressDialog(false);
	}

	@Override
	public void update(Observable observable, Object data) {
		if(data == null) return;
		DataResult result = (DataResult) data;
		
		if(result.getMethodName() == Methods.getQuizResult) {
			hideProgressDialog();
			QuizResultContent content = (QuizResultContent) result.getData();
			if(content != null) {
				findViewById(R.id.quiz_reward_frame).setVisibility(content.isWinner()?View.VISIBLE:View.INVISIBLE);
				findViewById(R.id.quiz_redemption_frame).setVisibility(content.isWinner()?View.VISIBLE:View.INVISIBLE);
				((TextView)findViewById(R.id.quiz_reward)).setText(content.getRewardProduct());
				String starDate = DateFormat.getDateFormat(this).format(new Date(content.getStartRedeem()));
				String endDate = DateFormat.getDateFormat(this).format(new Date(content.getEndRedeem()));
				((TextView)findViewById(R.id.quiz_reward_redemption)).setText(String.format("%s - %s", starDate,endDate)); 
				((TextView)findViewById(R.id.quiz_correct_answer)).setText(Long.toString(content.getPoint())); 
			}
		}
		
	}

	@Override
	public void onClick(View view) {
		
		if(view.getId() == R.id.btn_next) {
			Intent intent = new Intent(this, QuizExpiredDetail.class);
			int indexActivity = getTabActivityIndex();
			intent.putExtra(IntentKey.ACTIVITY_NAME,  indexActivity);
			intent.putExtra(Constant.KEY_QUIZ_ID,mQuiz.getId());
			startActivity(intent);
			finish();
		}
	}

}
